package com.weaver.drools.common.controller;

import java.io.IOException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.weaver.drools.common.drools.executor.RuleExecutor;
import com.weaver.drools.common.entity.ActCondition;
import com.weaver.drools.common.entity.UUsrUser36;
/**
* @application name:基础Controller
* @timer: 2018-10-30
* @author zhouxinlei
*/
public class CommonController {
	
	public static Subject getSubject() {
		return SecurityUtils.getSubject();
	}
	public UUsrUser36 getSessionUserBean() {
		return (UUsrUser36) getSubject().getPrincipal();
	}

	public Session getSession(Boolean flag) {
		return getSubject().getSession(flag);
	}

	public void login(AuthenticationToken token) {
		getSubject().login(token);
	}
	
	public HttpServletRequest getRequest() {
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
		return request;
	}
	public HttpServletResponse getResponse() {
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletResponse response = requestAttributes.getResponse();
		return response;
	}
	
	public void writejson(String json) {
		getResponse().setContentType("application/json;charset=utf-8");
		if (json != null) {
			try {
				getResponse().getWriter().write(json);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void writejsonAndSession(String json) {
		getResponse().setContentType("application/json;charset=utf-8");
		if (json != null) {
			try {
				getResponse().getWriter().write(json);
				getRequest().getSession(true).setAttribute("data", json);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static Date strToDateLong(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}
	
	public void BusinessRuleExecutor(String agendaGroup,Object object,Map<String,Object> map) {
		RuleExecutor.execute("inpection_check",object,map);
	}
}
